<?php

declare(strict_types=1);

namespace App\Models;

use App\Helpers\Password;

/**
 * Modelo que representa la tabla de usuarios.
 */
class UserModel extends BaseModel
{
    protected array $relations = [
        'role' => [
            self::BELONGS_TO,
            RoleModel::class,
            'role_id',
            ['select' => ['id', 'name', 'description']],
        ],
    ];

    public function getTable(): string
    {
        return 'users';
    }

    protected function beforeInsert(BaseModel $self): void
    {
        parent::beforeInsert($self);

        $self->password = Password::encrypt($self->password);
        $self->is_active ??= true;
    }

    protected function beforeUpdate(BaseModel $self): void
    {
        parent::beforeUpdate($self);

        if (isset($self->password)) {
            $self->password = Password::encrypt($self->password);
        }
    }
}
